/*
 * Copyright (C) 2012 Michael Widmer <widmm8@bfh.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.navigation.point;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import ch.bfh.aeneas.R;
import ch.bfh.aeneas.exchange.data.Node.NodeType;
import ch.bfh.aeneas.exchange.intent.AeneasIntent;
import ch.bfh.aeneas.navigation.app.NavigationActivity;
import ch.bfh.aeneas.navigation.point.PointService.PointServiceBinder;

/**
 * PointMe adds a new POI to the storage.
 * @author Michael Widmer <widmm8@bfh.ch>
 */
public class PointMe extends NavigationActivity {

    protected ServiceConnection mPointConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName arg0, IBinder arg1) {
            mPointService = (PointService) ((PointServiceBinder) arg1).getService();
            mPointService.addOnStateListener(mStateListener);
            mPointStarted = true;
        }

        public void onServiceDisconnected(ComponentName arg0) {
        }
    };
    
    private final static String LOG_TAG = "PointMe";
    
    private PointService mPointService;
    private boolean mPointStarted;
    private Location mLocToAdd;
    private int mCurrentStep;
    
    private String mPoiName;
    private NodeType mPoiType;
    
    private final static int STEP_NONE = -1;
    private final static int STEP_NAME = 0;
    private final static int STEP_TYPE = 1;
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        mPointStarted = false;
        
        mCurrentStep = -1;
        
        Intent startUp = getIntent();
        if(startUp.hasExtra("place")) {
            mLocToAdd = (Location) startUp.getParcelableExtra("place");
        }
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        
        if(resultCode == Activity.RESULT_OK) {
            
            if(requestCode == STEP_NAME) {
                
                mPoiName = data.getStringExtra("input");
                mCurrentStep = STEP_NAME;
                
            } else if(requestCode == STEP_TYPE) {
                
                mPoiType = NodeType.valueOf(data.getStringExtra("selectedValue"));
                mCurrentStep = STEP_TYPE;
                
            }            
        }       
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onResume() {
        super.onResume();
        
        if(mLocToAdd != null) {
            Log.i(LOG_TAG, "loc is not null: "+mLocToAdd.getLongitude()+"/"+mLocToAdd.getLatitude());
            if(mCurrentStep == STEP_NONE) {
                askForName();
            } else if(mCurrentStep == STEP_NAME) {
                /*askForType();
            } else if(mCurrentStep == STEP_TYPE) {*/
                bindService(new Intent(this, PointService.class), mPointConnection, BIND_AUTO_CREATE);
            }
        }        
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onPause() {
        if(mPointStarted) {
            mPointStarted = false;
            mPointService.removeOnResultsListener(mResultsListener);
            mPointService = null;
            unbindService(mPointConnection);
        }        
        super.onPause();
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    protected void onPositionConnected() {
        speech(getString(R.string.state_data_gets_loaded));
        if(mLocToAdd == null) {
            startListening();
        }
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    public void onPositionReceivedFirst(Location l) {
        if(mLocToAdd == null) {
            mLocToAdd = l;
            askForName();
        }
    }
    
    @Override
    /**
     * {@inheritDoc}
     */
    protected void onStateReady() {
        
        mPointService.addPoi(mPoiName, mLocToAdd);
        speech(getString(R.string.state_data_saved));
        finish();
    }
    
    protected void askForName() {
        Intent i = new Intent(AeneasIntent.ACTION_INPUT);
        i.addCategory(AeneasIntent.CATEGORY_DATA);
        i.putExtra("title", getString(R.string.point_title_string_input));
        
        startActivityForResult(i, STEP_NAME);
    }
    
    protected void askForType() {
        Intent i = new Intent(AeneasIntent.ACTION_SELECT);
        i.addCategory(AeneasIntent.CATEGORY_STRING);
        
        i.putExtra("values", NodeType.toStringArray());
        
        startActivityForResult(i, STEP_TYPE);
    }
}
